package com.loop.unclelend.ui.presenter

import com.loop.unclelend.R
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.fragment.Fm_Home
import com.loop.unclelend.ui.fragment.Fm_Repay
import com.loop.unclelend.ui.view.IView_Main
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/9 16:02
 * @Annotation：主页
 */
class Pre_Main(private val iView: IView_Main): ProPresenter(iView) {

    private val tabTitleArray = arrayListOf(
        getString(R.string.main_tab_home),
        getString(R.string.main_tab_payback)
    )
    private val tabImageNormal = arrayListOf(
        R.mipmap.icon_tab_home_normal,
        R.mipmap.icon_tab_payback_normal
    )

    private val tabImageSelect = arrayListOf(
        R.mipmap.icon_tab_home_select,
        R.mipmap.icon_tab_payback_select
    )

    //定义tab的每个页面
    private val tabList = arrayListOf<JSONObject>()
    private val fragments = ArrayList<ProFragment>()
    private var curtIndex = 0

    /**
     * 加载Tabbar数据
     */
    fun loadUncleMainTabList(){
        //tab
        for (i in 0 until tabTitleArray.size) {
            val tabObj = JSONObject()
            tabObj.put("tabId", i)
            tabObj.put("tabTitle", tabTitleArray[i])
            tabObj.put("tabImageSelect", tabImageSelect[i])
            tabObj.put("tabImageNormal", tabImageNormal[i])
            tabObj.put("isSelect", false)
            tabList.add(tabObj)
        }
        iView.showUncleTabList(tabList)
    }

    /**
     * 加载FragmentList
     */
    fun loadUncleMainFragmentList(){
        fragments.add(Fm_Home())//首页
        fragments.add(Fm_Repay())//待还款

        //默认选中第1个
        setUncleCurrentItemIndex(0)
    }

    /**
     * 切换Tab
     */
    fun setUncleCurrentItemIndex(index: Int) {
        curtIndex = index
        for (tabObj in tabList) {
            when (tabObj.optInt("tabId")) {
                index -> {
                    tabObj.put("isSelect", true)
                }
                else -> {
                    tabObj.put("isSelect", false)
                }
            }
        }
        iView.notifyUncleTabDataChanged()

        val fragment = fragments[curtIndex]
        iView.showUncleFragment(fragment,curtIndex)
    }

}